//
//  CPYListView.m
//  TTMusic
//
//  Created by chenpengyu on 16/4/9.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "CPYListView.h"
#import "CPYListViewTableViewCell.h"
@implementation CPYListView

+ (instancetype)playListView {
    static CPYListView *playListView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        playListView = [[CPYListView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, SysHeight)];
        playListView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
//        playListView.backgroundColor = [UIColor redColor];
//        COLOR(232, 233, 234)
    });
    return playListView;
}

    
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataSource = [NSMutableArray array];
        self.currentLocal = 0;
        [self creatTableView];

    }
    return self;
}
- (void)creatTableView {
    self.tableView = [[TTMusicBaseTableView alloc] initWithFrame:CGRectMake(0, SysHeight - 360, self.bounds.size.width, 360) style:UITableViewStylePlain];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    [_tableView registerClass:[CPYListViewTableViewCell class] forCellReuseIdentifier:@"CPYListViewTableViewCellIdentifier"];
    _tableView.backgroundColor = [UIColor clearColor];
    [self addSubview:_tableView];
    UIButton *hideButton = [UIButton buttonWithType:UIButtonTypeSystem];
    hideButton.frame = CGRectMake(0, 0, SysWidth, SysHeight - 414);
    hideButton.backgroundColor = [UIColor clearColor];
    [hideButton addTarget:self action:@selector(hideButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:hideButton];
    
}
- (void)setSongsModel:(CPYSONGSModel *)songsModel {
    _songsModel = songsModel;
    [_dataSource removeAllObjects];
    for (CPYDiscoveryPageSongListModel *songDetailModel in _songsModel.songs) {
        [self.dataSource addObject:songDetailModel];
    }
    if (_dataSource.count < 6) {
        _tableView.frame = CGRectMake(0, SysHeight - 50 - (_dataSource.count + 1) * 40, SysWidth, (_dataSource.count + 1) * 40);
    }
    [_tableView reloadData];
    NSLog(@"%ld", _dataSource.count);
}
- (void)hideButtonAction:(UIButton *)sender {
    self.hidden = YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CPYListViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CPYListViewTableViewCellIdentifier"];
//    cell.backgroundColor = COLOR(232, 233, 234);
//    cell.current = _currentLocal == indexPath.row;
    cell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    CPYDiscoveryPageSongListModel *songDetailModel = _dataSource[indexPath.row];
    
    NSRange range = [[NSString stringWithFormat:@"%@ / %@", songDetailModel.name, songDetailModel.singerName] rangeOfString:songDetailModel.name];
    NSMutableAttributedString *astr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ / %@", songDetailModel.name, songDetailModel.singerName]];
    [astr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:range];
    [astr setAttributes:[NSMutableDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, [UIFont systemFontOfSize:20], NSFontAttributeName, nil] range:range];
    cell.songAndSingerLabel.attributedText = astr;
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SysWidth, 40 * FitWidth)];
    view.backgroundColor = [UIColor colorWithWhite:0.937 alpha:1.000];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 250, 40)];
    [self updateFrame:label];
    [view addSubview:label];
    label.textColor = FONTCOLORHUI;
    NSMutableAttributedString *astr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"正在播放/%ld首", _songsModel.songs.count]];
    
    [astr addAttribute:NSForegroundColorAttributeName value:FONTCOLORHEI range:NSMakeRange(0, 4)];
    label.attributedText=astr;
    
    return view;
}
- (void)updateFrame:(UIView *)view{
    
    CGRect rect = view.frame;
    view.frame = CGRectMake(rect.origin.x * FitWidth, rect.origin.y * FitWidth, rect.size.width * FitWidth, rect.size.height * FitWidth);
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    _currentLocal = indexPath.row;
    [_tableView reloadData];
    [TTMusicAVPlayer shareTTMusicAVPlayer].local = indexPath.row;
    [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:_songsModel];
    self.hidden = YES;
}
- (void)setCurrentLocal:(NSInteger)currentLocal {
    _currentLocal = currentLocal;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
